package com.onlinelearn;


import com.j256.simplemagic.ContentInfo;
import com.j256.simplemagic.ContentInfoUtil;
import io.minio.GetObjectArgs;
import io.minio.MinioClient;
import io.minio.RemoveObjectArgs;
import io.minio.UploadObjectArgs;
import io.minio.errors.*;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.io.IOUtils;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;

import java.io.*;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

@SpringBootTest
class testChunk {

    MinioClient minioClient =
            MinioClient.builder()
                    .endpoint("http://125.91.106.213:9000")
                    .credentials("htj666", "htjhtj666")
                    .build();

    //测试文件分块方法
    @Test
    public void testChunk() throws IOException {
//        准备文件
        File file = new File("D:\\360Downloads\\a\\a.wmv");

        //准备输入流读取文件,并且分块写入到指定目录
        RandomAccessFile randomAccessFile = new RandomAccessFile(file,"rw");

//        分块大小
        long chunkSize = 1024 * 1024 * 5;

//        每次读取的字节数
        byte[] bytes = new byte[1024];

//        分块数
        long ceil = (long) Math.ceil(randomAccessFile.length() * 1.0 / chunkSize);

        int len = -1;
        FileOutputStream fileOutputStream = null;
        for (int i = 0; i < ceil; i++) {
            File temp = new File("D:\\360Downloads\\a\\test\\"+i);
            RandomAccessFile randomAccessFile1 = new RandomAccessFile(temp,"rw");
            while((len = randomAccessFile.read(bytes)) != -1){
                randomAccessFile1.write(bytes,0,len);
                if(temp.length() >= chunkSize){
                    break;
                }
            }
        }
        randomAccessFile.close();
    }

    public void Chunk(String sourceFile,String targetDir){
        //        准备文件
        File file = new File(sourceFile);
        if(!file.exists()){
            System.out.println("源文件不存在");
        }

        //准备输入流读取文件,并且分块写入到指定目录
        RandomAccessFile randomAccessFile = null;
        try {
//            随机流,用于读取文件到内存,并且写入文件到磁盘
            randomAccessFile = new RandomAccessFile(file,"rw");
            //        分块大小 此处为1M
            long chunkSize = 1024 * 1024 * 1;

//        每次读取的字节数
            byte[] bytes = new byte[1024];

//        分块数   向上取整
            long ceil = (long) Math.ceil(randomAccessFile.length() * 1.0 / chunkSize);
//          分块文件放在指定目录
            File dir = new File(targetDir);
            if(!dir.exists()){
                dir.mkdirs();
            }
            int len = -1;
            for (int i = 0; i < ceil; i++) {
                File temp = new File(sourceFile+i);
                RandomAccessFile randomAccessFile1 = new RandomAccessFile(temp,"rw");
                while((len = randomAccessFile.read(bytes)) != -1){
                    randomAccessFile1.write(bytes,0,len);
                    if(temp.length() >= chunkSize){
                        break;
                    }
                }
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(randomAccessFile != null){
                try {
                    randomAccessFile.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }

    //测试文件合并方法
    @Test
    public void testMerge() throws IOException {
//        找到源文件路径,循环所有的分块文件,输出到一个文件
        File file = new File("D:\\360Downloads\\a\\test");
        File[] files = file.listFiles();

        RandomAccessFile randomAccessFile = new RandomAccessFile("D:\\360Downloads\\a\\b.wmv","rw");

//        确保合并的顺序
        List<File> list = Arrays.asList(files);
        Collections.sort(list, (a,b) -> Integer.valueOf(a.getName()) - Integer.valueOf(b.getName()));
        RandomAccessFile randomAccessFile1 = null;
        for (File temp : list){
            randomAccessFile1 = new RandomAccessFile(temp,"rw");
            int len = -1;
            byte[] bytes = new byte[1024];
            while ((len = randomAccessFile1.read(bytes)) != -1){
                randomAccessFile.write(bytes,0,len);
            }
        }
    }

}
